/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain.shell;

import java.util.Map;

/**
 *
 * @author eisental
 */
public class MetaDataObject extends ShellObject {
    public MetaDataObject(DriveShell shell) {
        super("metadata", "meta", "meta", shell);
    }

    @Override
    public boolean isActionSupported(Action action) {
        return true;
    }

    @Override
    public void make(String command) throws IllegalArgumentException {
        // make meta key: value
        String[] pair = command.split(":");
        if (pair.length<2)
            throw new IllegalArgumentException("Bad syntax for make meta: " + "make meta " + command);
        if (shell.getActiveChain().getMetadataMap().containsKey(pair[0]))
            throw new IllegalArgumentException("Metadata key " + pair[0] + " already exists.");
        shell.getActiveChain().putMetadata(pair[0], pair[1]);
        out.println("Added meta key value pair:\n\t" + pair[0] + ": " + pair[1] + "\n\t\tto chain " + shell.getActiveChain().getName());
    }

    @Override
    public void remove(String command) throws IllegalArgumentException {
        if (shell.getActiveChain().getMetadataMap().containsKey(command))
            shell.getActiveChain().removeMetadata(command);
        else throw new IllegalArgumentException("Meta key " + command + " does not exist.");
        out.println("Removed meta data " + command + " from chain " + shell.getActiveChain().getName());
    }

    @Override
    public void set(String command) throws IllegalArgumentException {
        String[] pair = command.split(":");
        if (pair.length<2)
            throw new IllegalArgumentException("Bad syntax for make meta: " + "make meta " + command);
        if (!shell.getActiveChain().getMetadataMap().containsKey(pair[0]))
            throw new IllegalArgumentException("Metadata key " + pair[0] + " does not exist.");
        else shell.getActiveChain().putMetadata(pair[0], pair[1]);
    }

    @Override
    public void list() throws IllegalArgumentException {
        Map<String,String> metadata = shell.getActiveChain().getMetadataMap();

        if (metadata.isEmpty()) out.println("Chain " + shell.getActiveChain().getPath() + " does not have any meta data.");
        else {
            out.println("Metadata for chain " + shell.getActiveChain().getPath() + ":");

            for (String key : metadata.keySet())
                out.println("\t** " + key + ": " + metadata.get(key) + " ;");
        }
    }

    @Override
    public void inspect(String command) throws IllegalArgumentException {
        Map<String,String> metadata = shell.getActiveChain().getMetadataMap();

        if (metadata.containsKey(command))
            out.println(" - " + command + ": " + metadata.get(command));
    }

}
